﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.datatables;
using l1cs.server.serverpackets;
using l1cs.server.model;

namespace l1cs.server.clientpackets
{
    public class C_DeleteChar : ClientBasePacket
    {
        private const String _C_DELETE_CHAR = "[C] RequestDeleteChar";

        private static Logger _log = Logger.getLogger( typeof( C_DeleteChar ).FullName );

        public C_DeleteChar( byte[] decrypt, ClientThread client )
            : base( decrypt )
        {
            String name = readS();
            L1PcInstance pc = CharacterTable.getInstance().restoreCharacter( name );
            if ( pc != null && pc.getLevel() >= 30
                    && Config.DELETE_CHARACTER_AFTER_7DAYS )
            {
                if ( pc.getType() < 32 )
                {
                    if ( pc.isCrown() )
                    {
                        pc.setType( 32 );
                    }
                    else if ( pc.isKnight() )
                    {
                        pc.setType( 33 );
                    }
                    else if ( pc.isElf() )
                    {
                        pc.setType( 34 );
                    }
                    else if ( pc.isWizard() )
                    {
                        pc.setType( 35 );
                    }
                    else if ( pc.isDarkelf() )
                    {
                        pc.setType( 36 );
                    }
                    else if ( pc.isDragonKnight() )
                    {
                        pc.setType( 37 );
                    }
                    else if ( pc.isIllusionist() )
                    {
                        pc.setType( 38 );
                    }
                    // ID,PASS後の告知クリック時に削除メソッドを実行
                    pc.setDeleteTime( DateTime.Now.AddDays( 7 ) );// 7日後
                    pc.save(); // DBにキャラクター情報を書き込む
                }
                else
                {
                    if ( pc.isCrown() )
                    {
                        pc.setType( 0 );
                    }
                    else if ( pc.isKnight() )
                    {
                        pc.setType( 1 );
                    }
                    else if ( pc.isElf() )
                    {
                        pc.setType( 2 );
                    }
                    else if ( pc.isWizard() )
                    {
                        pc.setType( 3 );
                    }
                    else if ( pc.isDarkelf() )
                    {
                        pc.setType( 4 );
                    }
                    else if ( pc.isDragonKnight() )
                    {
                        pc.setType( 5 );
                    }
                    else if ( pc.isIllusionist() )
                    {
                        pc.setType( 6 );
                    }
                    pc.setDeleteTime( new DateTime( 0 ) ); // nullの代用
                    pc.save(); // DBにキャラクター情報を書き込む
                }
                client.sendPacket( new S_DeleteCharOK( S_DeleteCharOK.DELETE_CHAR_AFTER_7DAYS ) );
                return;
            }

            if ( pc != null )
            {
                L1Clan clan = L1World.getInstance().getClan( pc.getClanname() );
                if ( clan != null )
                {
                    clan.delMemberName( name );
                }
            }
            CharacterTable.getInstance().deleteCharacter( client.getAccountName(), name );

            client.sendPacket( new S_DeleteCharOK( S_DeleteCharOK.DELETE_CHAR_NOW ) );
        }

        override
        public String getType()
        {
            return _C_DELETE_CHAR;
        }
    }
}